/*
 * @Author: RanYang 
 * @Date: 2018-12-26 12:39:47 
 * @Last Modified by: RanYang
 * @Last Modified time: 2018-12-27 09:30:22
 */
const express = require('express')
const utils = require('utility')
const fs = require("fs")
const multer = require('multer')
const Router = express.Router()
const model = require('../model')
const USER = model.getModel('user')

const _filter = { 'pwd': 0, '__v': 0 }
const upload = multer({ dest: 'E:/esay-do-2018-upload/upload-temp' })

Router.get('/list', (req, res) => {
    const { userid } = req.cookies
    const { type } = req.query
    if (!userid) return res.json({ code: 2, msg: '没有登录！' }) 
    USER.find({ type }, _filter,(err, doc) => {
        if (err) {
            console.log(err)
        } else {
            if (doc) {
                return res.json({ code: 0, data:{ list: doc } })
            } else {
                res.json({ code: 1 })
            }
        }
    })
})

/**
 * 用户注册
 * @param user
 * @param pwd
 * @param type
 */

Router.post('/register', (req, res) => {
    const { user, pwd, type } = req.body
    USER.findOne({user}, (err, doc) => {
        if (doc) {
            return res.json({ code: 1, msg: '该用户名已经存在!' })
        } 
        // create新建不能读取_id 
        // USER.create({user, pwd: md5Pwd(pwd), type}, (err, doc) => {
        //     if (err) {
        //         return res.json({ code:1, msg:'后端出错了！' })
        //     }
        //     return res.json({code:0})
        // })
        const userModel = new USER({ user, pwd: md5Pwd(pwd), type })
        userModel.save((err, doc) => {
            if (err) {
                return res.json({ code: 1, msg: '后端出错了！' })
            }
            const { _id, user, type }  = doc
            res.cookie('userid', _id)
            return res.json({ code: 0, data: { _id, user, type } })
        })
    })
})

/**
 * 用户登录
 * @param user
 * @param pwd
 */

Router.post('/login', (req, res) => {
    const { user, pwd } = req.body
    USER.findOne({ user, pwd: md5Pwd(pwd) }, _filter, (err, doc) => {
        if (doc) {
            res.cookie('userid', doc._id)
            res.json({ code: 0, data: doc })
        } else {
            res.json({ code: 1, msg: '用户名或者密码错误！' })
        }
    })
})

/**
 * 获取用户信息
 */

Router.get('/info', (req, res) => {
    const { userid } = req.cookies
    if (!userid) {
        return res.json({ code: 1 })
    }
    USER.findOne({ _id: userid }, _filter, (err, doc) => {
        if (doc) {
            return res.json({ code: 0, data: doc })
        } else {
            return res.json({ code: 1, msg: '服务端出错了！' })
        }
    })
})

/**
 * 新增boss基本信息
 * @param {file} avatar
 * @param company
 * @param desc
 * @param money
 * @param title
 */

 Router.post('/userUpdate', upload.any(),(req, res) => {
    const { company, desc, money, title } = req.body
    const { userid } = req.cookies
    const errResult = { code: 1, msg: '服务端错误！' }
    if (!userid) {
        return res.json({ code: 2, msg: '没有登录状态 ！' })
    }
    if (req.files && req.files.length === 0) {
        return res.json({ code: 1, msg: '上传文件不能为空！'})
    }
    const newfilename = req.files[0].filename+req.files[0].originalname 
    const des_file = "E:/esay-do-2018-upload/upload/" + newfilename

    fs.readFile(req.files[0].path,(err, data) => {
        fs.writeFile(des_file, data, err => {
            if (!err) {
                USER.findByIdAndUpdate(userid, { company, desc, money, title, avatar: newfilename }, (err, doc) => {
                    if (doc) {
                        const { user, avatar, type } = doc
                        res.json({ code: 0, data: { user, avatar, type } })
                    } else {
                        res.json(errResult)
                    }
                })
            } else {
                res.json(errResult)
            }
        })
    })
 })

function md5Pwd(pwd) {
    const salt = 'iosde23231=-=2ss@sd2!@!,.sdf-,.d232'
    return utils.md5(utils.md5(salt + pwd))
}

module.exports = Router